package good

import (
	"enjoy_wangyashuo/internal/model/mysql"
	"errors"
	"fmt"
	"gorm.io/gorm"
)

// 商品分类表的结构体
type GoodsCategory struct {
	gorm.Model
	Icon     string `gorm:"type:varchar(255);comment:分类图片链接;"`
	Lv       int64  `gorm:"type:int(10);comment:分类等级;"`
	PID      int64  `gorm:"type:int(10);comment:父级分类ID;"`
	Name     string `gorm:"type:varchar(255);comment:分类名称;"`
	Location int64  `gorm:"type:int(10);comment:分类位置;"`
}
type CategoryLists struct {
	ID       int64            `json:"id"`
	Icon     string           `json:"icon"`
	Lv       int64            `json:"lv"`
	PID      int64            `json:"PID"`
	Name     string           `json:"name"`
	Location int64            `json:"location"`
	Children []*CategoryLists `json:"children"`
}

// 分类位置列表
func (g *GoodsCategory) CategoryList() (infoLists []CategoryLists, err error) {
	//分页
	var list []GoodsCategory
	return infoLists, mysql.WithMysql(func(db *gorm.DB) error {
		//如果没有分页位置 就代表是首页 查询全部
		fmt.Println("4444", list)
		if g.Location == 0 {
			err = db.Model(&g).Find(&list).Error
			fmt.Println("333", list)
		} else {
			err = db.Where("location = ?", g.Location).Find(&list).Error
		}
		fmt.Println("222", list)
		//如果有查询当前分页位置的数据
		for _, v := range list {
			infoLists = append(infoLists, CategoryLists{
				ID:       int64(v.ID),
				Icon:     v.Icon,
				Lv:       v.Lv,
				PID:      v.PID,
				Name:     v.Name,
				Location: v.Location,
			})
		}
		return nil
	})
}

// GetCategoryTree 获取分类树，只返回顶级分类及其子分类
func (g *GoodsCategory) GetCategoryList() (list []CategoryLists, err error) {
	return list, mysql.WithMysql(func(db *gorm.DB) error {
		// 获取所有分类
		var categories []GoodsCategory
		if err := db.Find(&categories).Error; err != nil {
			return err
		}
		// 构建分类映射表，存储指向全部分类数据的指针
		categoryMap := make(map[int64]*CategoryLists)
		for _, val := range categories {
			categoryMap[int64(val.ID)] = &CategoryLists{
				Icon:     val.Icon,
				Lv:       val.Lv,
				PID:      val.PID,
				Name:     val.Name,
				Location: val.Location,
				Children: []*CategoryLists{},
			}
		}
		// 遍历分类，构建层级关系
		for _, category := range categories {
			if category.PID != 0 {
				// 子分类，找到其父分类并添加到Children列表中
				if parentCategory, ok := categoryMap[category.PID]; ok {
					parentCategory.Children = append(parentCategory.Children, categoryMap[int64(category.ID)])
				}
			}
		}
		// 将顶级分类及其所有子分类赋值给结果列表
		for _, topLevelCategory := range categoryMap {
			if topLevelCategory.PID == 0 {
				// 只处理顶级分类
				list = append(list, *topLevelCategory)
			}
		}
		return nil
	})
}

// 分类查询商品
func (g *GoodsCategory) GetGoodsInfoByTypeID() {
	var list []GoodsCategory
	mysql.WithMysql(func(db *gorm.DB) error {
		err := db.Where("id = ?", g.ID).First(&g).Error
		if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
			return errors.New("服务器错误")
		}
		//存储全部的pid
		var ids []int64
		for _, val := range list {
			ids = append(ids, int64(val.ID))
		}
		var categoryinfo []GoodsCategory
		err = db.Where("id in ?", ids).Find(&categoryinfo).Error
		var pids []int64
		for _, val := range categoryinfo {
			pids = append(pids, val.PID)
		}
		return nil
	})
}
